package com.sophie.controller;

import com.sophie.entity.Comment;
import com.sophie.entity.User;
import com.sophie.service.ICommentService;
import com.sophie.service.impl.CommentServiceImpl;
import com.sophie.util.ResultVO;
import com.sophie.util.SendJSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "CommentServlet", value = "/CommentServlet")
public class CommentServlet extends BaseServlet {
    ICommentService commentService = new CommentServiceImpl();

    //CommentServlet?method=addComment
    protected void addComment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("------------------------------->添加评论");
        String zoneid = request.getParameter("id");
        String commenttext = request.getParameter("commenttext");
        System.out.println("commenttext = " + commenttext);;
        User user = (User) request.getSession().getAttribute("user");
        //
        Comment comment = new Comment(null, Integer.parseInt(zoneid), new Date(), user.getId(), commenttext);
        ResultVO resultVO = commentService.addComment(comment);
        SendJSONUtil.send(resultVO,response);
    }

    //CommentServlet?method=getCommentByZoneId
    protected void getCommentByZoneId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("------------------------------->根据动态id查询评论");
        String zoneid = request.getParameter("zoneid");
        System.out.println("zoneid = " + zoneid);
        ResultVO resultVO = commentService.getCommentByZoneId(Integer.parseInt(zoneid));
        System.out.println("resultVO.getData() = " + resultVO.getData());
        SendJSONUtil.send(resultVO.getData(),response);
    }

    //CommentServlet?method=countComment
    protected void countComment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("------------------------------->计算评论数");
        String zoneid = request.getParameter("zoneid");
        System.out.println("zoneid = " + zoneid);
        //
        ResultVO resultVO = commentService.countCommentByZoneId(Integer.parseInt(zoneid));
        SendJSONUtil.send(resultVO.getData(),response);
    }

    //CommentServlet?method=none
    protected void none(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("------------------------------->");
    }
}